package com.umakr.ax.common.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Maps;
import com.umakr.ax.common.mapper.PluginCouponMapper;
import com.umakr.ax.common.model.PluginCoupon;
import com.umakr.ax.common.vo.PluginCouponListVO;
import com.umakr.ax.core.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * ${table.comment} 服务实现类
 * </p>
 *
 * @author Umakr-Code-Generator
 * @since 2017-12-30
 */
@Service
public class PluginCouponService extends BaseService<PluginCouponMapper, PluginCoupon>{

    @Autowired
    PluginCouponVerPepleService pepleService;
    @Autowired
    Environment env;

    public Object listCouponsByHxrTel(String hxrTel){
        String aliUrl = env.getProperty("oss.img.path");
        long dateTime = System.currentTimeMillis();
        Page<PluginCouponListVO> page = baseMapper.getCouponsByHxrTel(hxrTel);

        for(PluginCouponListVO vo : page.getRecords()){

            if(dateTime <= vo.getEndTime().getTime()){
                vo.setStatus(1);
                vo.setStatusName("有效");
            }else{
                vo.setStatus(0);
                vo.setStatusName("过期");
            }

            if(vo.getSellerLogo() != null) {
                vo.setSellerLogo(aliUrl+vo.getSellerLogo());
            }

        }

        return page;
    }

    public Object delPluginCoupon(Long id, Integer delStatus) {
        int errorCode = 0 ;
        String msg;
        Map<String,Object> data = Maps.newLinkedHashMap();
        baseMapper.delPluginCoupon(id, delStatus);
        errorCode = 1 ;
        msg = "操作成功";
        data.put("result",1);
        return resultMap(data,errorCode,msg);
    }
}
